package com.yurun.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author 宫静雨
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill");
        this.strictInsertFill(metaObject, "createTime",
                LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "updatedTime",
                LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "createBy",
                () -> "X", String.class);
        this.strictInsertFill(metaObject, "updatedBy",
                () -> "X", String.class);
        log.info("end insert fill");
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill");
        this.strictInsertFill(metaObject, "updatedTime",
                LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "updatedBy",
                () -> "Y", String.class);
        log.info("end update fill");
    }
}